<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    ***************************************************************************
    Updated: 22/04/2009
    
    TODO: 
    1. "tns:BER" in When construct is not added yet
    2. boolean expressions construct has no output, need to deal with range selecting, config file maybe need to change
    3. need definition for WhenFor
    
    Change Logs:
    1. fixed default value output
    3. fixed typing error about ture/false (from T/F)
    2. add namespaces definition in every MD output part
    3. normalize spaces (removing leading or trailing spaces) of ontologicalMeaning
    4. fixed output if there's only 1 parameter for eventDefinition
    5. sequence number added
    6. add copying all content in configurationData in config file to output
    7. add a temporary root element in output xml for well-formed.
    
    
    Additional Info:
    1. there's no output for PassedInSOAPHeaders for example. since there's no corresponding MD-Additioanl Info in config file
    
    
    ****************************************************************************
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:MASC_Profile="http:///schemas/MASC_Profile/_OnYS4BTgEd6yO-ZURVEtWw/6"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" 
    xmlns:uml="http://www.eclipse.org/uml2/2.1.0/UML" 
    xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"  
    xmlns:masc-gp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp"  
    xmlns:masc-se="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se"   
    xmlns:masc-ex="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex"  
    xmlns:masc-om="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om" 
    xmlns:masc-sc="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc"  
    xmlns:masc-ap="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap"  
    xmlns:masc-mp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp"  
    xmlns:masc-up="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up"  
    xmlns:masc-config="http://www.nicta.com.au/projects/armature/masc-config"
    xmlns:wsa="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/wsa" 
    xmlns:QoSOntology="http://www.myontologyrepository.com/qosontology.owl" 
    xmlns:tns="${currentFileURL}" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output method="xml" indent="yes"/>
    
    <xsl:variable name="input" select="document('StockTradingGPA.xml')"></xsl:variable>
    <xsl:variable name="config" select="document('config.xml')"></xsl:variable>
    <xsl:variable name="xslt" select="document('MD_Transform.xsl')"></xsl:variable>
    <!-- define a newline variable for pretty output -->
    <xsl:variable name="newline"><xsl:text>&#10;</xsl:text></xsl:variable>
    <!-- define the target namespace -->
    <xsl:variable name="tns"><xsl:text>tns:</xsl:text></xsl:variable>
    
    <xsl:template match="/">
        <xsl:element name="root">
            <xsl:comment><xsl:text>This root element is for well-formed of output. Could be removed if we generate a corresponding output file with every Policy Assertion/Monitored Definition</xsl:text></xsl:comment>
            <xsl:value-of select="$newline"></xsl:value-of>
            <xsl:value-of select="$newline"></xsl:value-of>
            <xsl:apply-templates select="$input/xmi:XMI" />
        </xsl:element>
        <xsl:value-of select="$newline"></xsl:value-of>
    </xsl:template>
    
    
    <xsl:template match="xmi:XMI">
        <xsl:for-each select="$input/xmi:XMI/MASC_Profile:MonitorDef">
            <xsl:variable name="md-MASCID" select="./@MD-MASCID"></xsl:variable>
            <xsl:variable name="valueDataType" select="./@ValueDataType"></xsl:variable>
            <xsl:variable name="unit" select="./@Unit"></xsl:variable>
            <xsl:variable name="ontologicalMeaning" select="./@OntologicalMeaning"></xsl:variable>
            <xsl:variable name="md-managementParty" select="./@MD-ManagementParty"></xsl:variable>
            <xsl:variable name="md-When-TriggerEvents" select="./@MD-When-TriggerEvents"></xsl:variable>
            <xsl:variable name="baseElement" select="./@base_Element"></xsl:variable>
            <xsl:variable name="monitoredFor" select="./@monitoredFor"></xsl:variable>
            <xsl:variable name="SN"> <!-- define sequence number by MD's position -->
                <xsl:number value="position()" format="1"></xsl:number>
            </xsl:variable>
            
           
            <xsl:for-each select="$input//packagedElement/node">
                <xsl:variable name="name" select="@name"></xsl:variable>
                <xsl:variable name="xmiID" select="@xmi:id"></xsl:variable>

                <xsl:choose>
                    <xsl:when test="$xmiID=$baseElement">
                        
                        <!--
                            ***********************************************************

                                    varibales for monitoring definition constructs

                            ***********************************************************
                        -->
                        <!-- variables for ontological meaning -->
                        <xsl:variable name="GPA_MD_ON">	<!-- ontology name -->
                            <xsl:value-of select="normalize-space(substring-before($ontologicalMeaning, ':'))"></xsl:value-of>	
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_OC">	<!-- ontology construct -->
                            <xsl:value-of select="normalize-space(substring-after($ontologicalMeaning, ':'))"></xsl:value-of>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_OL">	<!-- ontology language -->
                            <xsl:value-of select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$md-MASCID]/@OntologicalLanguage"></xsl:value-of>
                        </xsl:variable>
                        
                        <!-- variables for monitored data items -->
                        <xsl:variable name="GPA_MD_MASCID">	<!-- monitoringDefinition MASCID -->
                            <xsl:value-of select="$md-MASCID"></xsl:value-of>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_VDT">	<!-- valueDataType -->
                            <xsl:value-of select="$valueDataType"></xsl:value-of>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_U">	<!-- unit -->
                            <xsl:value-of select="$unit"></xsl:value-of>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_MPN">	<!-- messagePartName -->
                            <xsl:value-of select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$md-MASCID]/@MessagePartName"></xsl:value-of>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_OMMASCID">
                            <xsl:text>OM-</xsl:text>
                            <xsl:value-of select="$GPA_MD_ON"></xsl:value-of>
                            <xsl:text>-</xsl:text>
                            <xsl:value-of select="$GPA_MD_OC"></xsl:value-of>
                        </xsl:variable>
                        
                        <!-- variables for states -->
                        <xsl:variable name="GPA_MD_SD">	<!-- state definition -->
                            <xsl:value-of select="substring-before(substring-after($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$md-MASCID]/@When-AllowedStates, '{'), '}')"></xsl:value-of>
                        </xsl:variable>
                        
                        <!-- variables for events -->
                        <xsl:variable name="GPA_MD_temp1">
                            <xsl:value-of select="$md-When-TriggerEvents"></xsl:value-of>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_ET">	<!-- event type -->
                            <xsl:value-of select="substring-after(substring-before($GPA_MD_temp1, '('), '{')"></xsl:value-of>
                        </xsl:variable>
                        
                        <xsl:variable name="GPA_MD_temp2">
                            <xsl:value-of select="substring-after(substring-before($GPA_MD_temp1, ')'), '(')"></xsl:value-of>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_ParamList" select="tokenize($GPA_MD_temp2,',')"></xsl:variable>
                        <xsl:variable name="GPA_MD_P1">
                            <xsl:value-of select="normalize-space($GPA_MD_ParamList[position()=1])"/>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_P2">
                            <xsl:value-of select="normalize-space($GPA_MD_ParamList[position()=2])"/>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_P3">
                            <xsl:value-of select="normalize-space($GPA_MD_ParamList[position()=3])"/>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_P4">
                            <xsl:value-of select="normalize-space($GPA_MD_ParamList[position()=4])"/>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_P5">
                            <xsl:value-of select="normalize-space($GPA_MD_ParamList[position()=5])"/>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_P6">
                            <xsl:value-of select="normalize-space($GPA_MD_ParamList[position()=6])"/>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_Event_ParamCount">
                            <xsl:value-of select="count($GPA_MD_ParamList)"></xsl:value-of>
                        </xsl:variable>
                        
                        <xsl:variable name="PN">	<!-- process name -->
                            <xsl:value-of select="$config/masc-config:XMIFile/masc-config:UMLDiagram/@UMLDiagramName"></xsl:value-of>
                        </xsl:variable>
                        <xsl:variable name="AN">	<!-- activity name -->
                            <xsl:value-of select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/@UMLElementName"></xsl:value-of>
                        </xsl:variable>
                        
                        <!-- variables for when constructs -->
                        <xsl:variable name="GPA_MD_WF">	<!-- When For -->	<!-- not finished -->
                            <xsl:text>WF</xsl:text>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_WFMASCID">	<!-- value of WhenFor MASCID -->	<!-- not finished -->	<!-- user GPA_MD_MASCID-GPA_MD_ET for now -->
                            <xsl:value-of select="$GPA_MD_MASCID"></xsl:value-of>
                            <xsl:text>-</xsl:text>
                            <xsl:value-of select="$GPA_MD_ET"></xsl:value-of>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_ED">	<!-- mascid of previously defined Event Definition Construct -->
                            <xsl:value-of select="$GPA_MD_ET"></xsl:value-of>
                            <xsl:text>-</xsl:text>
                            <xsl:value-of select="$GPA_MD_P1"></xsl:value-of>
                            <xsl:if test="normalize-space($GPA_MD_P2)!=''">
                                <xsl:text>-</xsl:text>
                                <xsl:value-of select="$GPA_MD_P2"></xsl:value-of>
                            </xsl:if>
                        </xsl:variable>
                        
                        <!-- variables for boolean expressions for monitoring definition -->
                        
                        
                        
                        <!-- variables for monitoring data collection -->
                        <xsl:variable name="GPA_MD_MO">	<!-- prefix for monitoring data collection MASCID -->
                            <xsl:text>MonitoringOf</xsl:text>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_PassedSOAPHeaders">	<!-- passed soap headers -->
                            <xsl:value-of select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$md-MASCID]/@PassedInSOAPHeaders"></xsl:value-of>
                        </xsl:variable>
                        
                        <!-- variables for action policy assertions configuring monitoring -->
                        <xsl:variable name="GPA_MD_APAM">	<!-- prefix for MASCID -->
                            <xsl:text>APAMonitor</xsl:text>
                        </xsl:variable>
                        <xsl:variable name="WSP">	<!-- web service provider/process name -->
                            <xsl:value-of select="$PN"></xsl:value-of>
                        </xsl:variable>
                        <xsl:variable name="GPA_MD_MPTY">
                            <xsl:value-of select="$md-managementParty"></xsl:value-of>
                        </xsl:variable>

                        
                        <!-- the output templte has to be here, cannot use attributes set -->
                        <!--
                            ***********************************************************
                            
                            TODO: define output
                            
                            ***********************************************************
                        -->
                        <xsl:comment>
                            <xsl:text>OUTPUT FOR MONITORING DEFINITION for UML Element: </xsl:text>
                            <xsl:value-of select="$name"/>
                            <xsl:value-of select="$newline"></xsl:value-of>
                            <xsl:text> The MASCID is: </xsl:text>
                            <xsl:value-of select="$GPA_MD_MASCID"/>
                        </xsl:comment>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        
                        <xsl:text disable-output-escaping="yes">
                            <![CDATA[<wsp:Policy xmlns:xs="http://www.w3.org/2001/XMLSchema "  
            				 xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"  
                             xmlns:masc-gp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp"  
                             xmlns:masc-se="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se"   
                             xmlns:masc-ex="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex"  
                             xmlns:masc-om="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om" 
                             xmlns:masc-sc="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc"  
                             xmlns:masc-ap="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap"  
                             xmlns:masc-mp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp"  
                             xmlns:masc-up="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up"  
            				 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            				 xsi:schemaLocation="http://schemas.xmlsoap.org/ws/2004/09/policy 
            				 C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy.xsd 
            				 http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp 
            				 C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-gp.xsd
            				 http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se 
            				 C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-se.xsd
            				 http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex 
            				 C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-ex.xsd 
            				 http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om 
            				 C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-sc.xsd 
            				 http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc 
            				 C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-om.xsd 
            				 http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap 
            				 C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-ap.xsd 
            				 http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up 
            				 C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-up.xsd 
            				 http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp 
            				 C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-mp.xsd"  
                             xmlns:tns="${currentFileURL}" 
            				 xmlns:wsbpel="wsbpel"   
            				 xmlns:wsdlfile="wsdlfile" 
            				 xmlns:ontology="0"  
            				 targetNamespace="Var1">]]> 
            				 &#10;
                        </xsl:text> 
                        
                        <xsl:comment> 
                            <xsl:text>Definition of OntologicalMeaning</xsl:text>
                        </xsl:comment>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:element name="masc-om:OntologicalMeaning" >
                            <xsl:attribute name="MASCID">	<!-- MASCID="OM-GPA_MD_ON-GPA_MD_OC" -->
                                <xsl:text>OM-</xsl:text>
                                <xsl:value-of select="$GPA_MD_ON"></xsl:value-of>
                                <xsl:text>-</xsl:text>
                                <xsl:value-of select="$GPA_MD_OC"></xsl:value-of>
                            </xsl:attribute>
                            <xsl:attribute name="OntologicalDefinition">	<!-- OntologicalDefinition="GPA_MD_ON:GPA_MD_OC" -->
                                <xsl:value-of select="$GPA_MD_ON"></xsl:value-of>
                                <xsl:text>:</xsl:text>
                                <xsl:value-of select="$GPA_MD_OC"></xsl:value-of>
                            </xsl:attribute>
                            <xsl:attribute name="OntologicalLanguage">	<!-- OntologicalLanguage="GPA_MD_OL" -->
                                <xsl:value-of select="$GPA_MD_OL"></xsl:value-of>
                            </xsl:attribute>
                        </xsl:element>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        
                        <xsl:comment> 
                            <xsl:text>Definition of Monitored Data Items</xsl:text>
                        </xsl:comment>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:element name="masc-gp:MonitoredDataItem">
                            <xsl:attribute name="MASCID">	<!-- MASCID="GPA_MD_MASCID" -->
                                <xsl:value-of select="$GPA_MD_MASCID"></xsl:value-of>
                            </xsl:attribute>
                            <xsl:attribute name="ValueDataType">	<!-- ValueDataType="GPA_MD_VDT" -->
                                <xsl:value-of select="$GPA_MD_VDT"></xsl:value-of>
                            </xsl:attribute>
                            <xsl:attribute name="Unit">	<!-- Unit="GPA_MD_U" -->
                                <xsl:value-of select="$GPA_MD_U"/>
                            </xsl:attribute>
                            <xsl:attribute name="MessagePartName">	<!-- MessagePartName="GPA_MD_MPN" -->
                                <xsl:value-of select="$GPA_MD_MPN"></xsl:value-of>
                            </xsl:attribute>
                        </xsl:element>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:element name="masc-om:OntologicalMeaningRef">
                            <xsl:attribute name="To">	<!-- To="tns:OM-GPA_MD_ON-GPA_MD_OC" -->
                                <xsl:value-of select="$tns"></xsl:value-of>
                                <xsl:text>OM-</xsl:text>
                                <xsl:value-of select="$GPA_MD_ON"></xsl:value-of>
                                <xsl:text>-</xsl:text>
                                <xsl:value-of select="$GPA_MD_OC"></xsl:value-of>
                            </xsl:attribute>
                        </xsl:element>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        
                        <xsl:comment> 
                            <xsl:text>Definition of States</xsl:text>
                        </xsl:comment>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:element name="masc-se:StateDefinition">
                            <xsl:attribute name="MASCID">	<!-- MASCID="GPA_MD_SD" -->
                                <xsl:value-of select="$GPA_MD_SD"></xsl:value-of>
                            </xsl:attribute>
                        </xsl:element>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:value-of select="$newline"></xsl:value-of>
                        
                        <!-- event constructs -->
                        <xsl:comment> 
                        	<xsl:text>Definition of Events</xsl:text>
                        </xsl:comment>
                		<xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:element name="masc-se:EventDefinition">
                            <xsl:attribute name="MASCID">	<!-- MASCID="GPA_MD_ET-GPA_MD_P1-GPA_MD_P2" up to 2 parameters would be shown-->
                                <xsl:value-of select="$GPA_MD_ED"></xsl:value-of>
                                <xsl:text>-</xsl:text>
                                <xsl:value-of select="$SN"></xsl:value-of>
                            </xsl:attribute>
                        	<!-- cases of events -->
                        	<xsl:choose>
                        		<!-- case1: ActivityEnded or ActivityStarted -->
                        		<xsl:when test="contains(lower-case($GPA_MD_ET), 'activityended')">
                        			<xsl:element name="masc-se:{$GPA_MD_ET}">
                        				<xsl:attribute name="ActivityName"><xsl:value-of select="$AN"></xsl:value-of></xsl:attribute>
                        				<xsl:attribute name="Successful"><xsl:value-of select="$GPA_MD_P2"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        		<xsl:when test="contains(lower-case($GPA_MD_ET), 'activitystarted')">
                        			<xsl:element name="masc-se:{$GPA_MD_ET}">
                        				<xsl:attribute name="ActivityName"><xsl:value-of select="$AN"></xsl:value-of></xsl:attribute>
                        				<xsl:attribute name="Successful"><xsl:value-of select="$GPA_MD_P2"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case2: MonitoredDataItemUpdated -->
                        		<xsl:when test="contains(lower-case($GPA_MD_ET), 'monitoreddataitemupdated')">
                        			<xsl:element name="masc-se:MonitoredDataItemUpdated">
                        				<xsl:attribute name="ItemID"><xsl:value-of select="$tns"></xsl:value-of><xsl:value-of select="$GPA_MD_P1"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case3: GoalStatisfaction -->
                        		<xsl:when test="contains(lower-case($GPA_MD_ET), 'goalsatisfaction')">
                        			<xsl:element name="masc-se:GoalStatisfaction">
                        				<xsl:attribute name="GoalPolicyAssertionID"><xsl:value-of select="$tns"></xsl:value-of><xsl:value-of select="$GPA_MD_P1"></xsl:value-of></xsl:attribute>
                        				<xsl:attribute name="Satisfied"><xsl:value-of select="$GPA_MD_P2"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case4: OperationInvocation -->
                        		<xsl:when test="contains(lower-case($GPA_MD_ET), 'operationinvocation')">
                        			<xsl:element name="masc-se:OperationInvocation">
                        				<xsl:element name="masc-sc:ScopeOperation">
                        					<xsl:element name="masc-sc:ServiceName">
                        						<xsl:attribute name="Name"><xsl:value-of select="$GPA_MD_P1"></xsl:value-of><xsl:text>:</xsl:text><xsl:value-of select="$GPA_MD_P2"></xsl:value-of></xsl:attribute>
                        						<xsl:attribute name="Completed"><xsl:value-of select="$GPA_MD_P5"></xsl:value-of></xsl:attribute>
                        						<xsl:attribute name="FaultsOccured"><xsl:value-of select="$GPA_MD_P6"></xsl:value-of></xsl:attribute>
                        					</xsl:element>
                        					<xsl:element name="masc-sc:EndpointName">
                        						<xsl:attribute name="Name"><xsl:value-of select="$GPA_MD_P1"></xsl:value-of><xsl:text>:</xsl:text><xsl:value-of select="$GPA_MD_P3"></xsl:value-of></xsl:attribute>
                        					</xsl:element>
                        					<xsl:element name="masc-sc:OperationName">
                        						<xsl:attribute name="Name"><xsl:value-of select="$GPA_MD_P1"></xsl:value-of><xsl:text>:</xsl:text><xsl:value-of select="$GPA_MD_P4"></xsl:value-of></xsl:attribute>
                        					</xsl:element>
                        				</xsl:element>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case5: MessageReceipt, MessageToBeSent and MessageSending -->
                        		<xsl:when test="starts-with(lower-case($GPA_MD_ET), 'message')">
                        			<xsl:element name="masc-se:{$GPA_MD_ET}">
                        				<xsl:element name="masc-sc:ScopeMessage">
                        					<xsl:element name="masc-sc:ServiceName">
                        						<xsl:attribute name="Name"><xsl:value-of select="$GPA_MD_P1"></xsl:value-of><xsl:text>:</xsl:text><xsl:value-of select="$GPA_MD_P2"></xsl:value-of></xsl:attribute>
                        						<xsl:attribute name="Completed"><xsl:value-of select="$GPA_MD_P5"></xsl:value-of></xsl:attribute>
                        					</xsl:element>
                        					<xsl:element name="masc-sc:EndpointName">
                        						<xsl:attribute name="Name"><xsl:value-of select="$GPA_MD_P1"></xsl:value-of><xsl:text>:</xsl:text><xsl:value-of select="$GPA_MD_P3"></xsl:value-of></xsl:attribute>
                        					</xsl:element>
                        					<xsl:element name="masc-sc:OperationName">
                        						<xsl:attribute name="Name"><xsl:value-of select="$GPA_MD_P1"></xsl:value-of><xsl:text>:</xsl:text><xsl:value-of select="$GPA_MD_P4"></xsl:value-of></xsl:attribute>
                        					</xsl:element>
                        					<xsl:element name="masc-sc:MessageName">
                        						<xsl:attribute name="Name"><xsl:value-of select="$GPA_MD_P5"></xsl:value-of></xsl:attribute>
                        					</xsl:element>
                        				</xsl:element>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case6: ActionCompletion -->
                        		<xsl:when test="contains(lower-case($GPA_MD_ET), 'actioncompletion')">
                        			<xsl:element name="masc-se:ActionCompletion">
                        				<xsl:attribute name="ActionPolicyAssertionID"><xsl:value-of select="$tns"></xsl:value-of><xsl:value-of select="$GPA_MD_P1"></xsl:value-of></xsl:attribute>
                        				<xsl:attribute name="Completed"><xsl:value-of select="$GPA_MD_P2"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case7: ScheduledEventOccurence -->
                        		<xsl:when test="contains(lower-case($GPA_MD_ET), 'scheduledeventoccurence')">
                        			<xsl:element name="masc-se:ScheduledEventOccurence">
                        				<xsl:attribute name="scheduleID"><xsl:value-of select="$tns"></xsl:value-of><xsl:value-of select="$GPA_MD_P1"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case8: ProcessInstanceStart and ProcessInstanceFinished -->
                        		<xsl:when test="starts-with(lower-case($GPA_MD_ET), 'processinstance')">
                        			<xsl:element name="masc-se:PIT">
                        				<xsl:attribute name="ProcessType"><xsl:value-of select="$tns"></xsl:value-of><xsl:value-of select="$GPA_MD_P1"></xsl:value-of></xsl:attribute>
                        				<xsl:attribute name="Completed"><xsl:value-of select="$GPA_MD_P2"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case9: PartyToBeInitialized and PartyToBeFinalized -->
                        		<xsl:when test="starts-with($GPA_MD_ET, 'partytobe')">
                        			<xsl:element name="masc-se:PIF">
                        				<xsl:attribute name="PartyName"><xsl:value-of select="$tns"></xsl:value-of><xsl:value-of select="$GPA_MD_P1"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case10: StateEntrance, StateTransitioning and StateExit -->
                        		<xsl:when test="(contains(lower-case($GPA_MD_ET), 'stateentrance')) or (contains(lower-case($GPA_MD_ET),'statetransitioning')) or (contains(lower-case($GPA_MD_ET), 'stateexit'))">
                        			<xsl:element name="masc-se:STTE">
                        				<xsl:attribute name="To"><xsl:value-of select="$tns"></xsl:value-of><xsl:value-of select="$GPA_MD_P1"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case11: PolicyAssertionAddition and PolicyAssertionDeletion -->
                        		<xsl:when test="starts-with(lower-case($GPA_MD_ET), 'policyassertion')">
                        			<xsl:element name="masc-se:PT">
                        				<xsl:attribute name="PolicyAssertionID"><xsl:value-of select="$tns"></xsl:value-of><xsl:value-of select="$GPA_MD_P1"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        		<!-- case12: MonitoredContextPropertyChanged -->
                        		<xsl:when test="contains(lower-case($GPA_MD_ET), 'monitoredcontextpropertychanged')">
                        			<xsl:element name="masc-se:MonitoredContextPropertyChanged">
                        				<xsl:attribute name="ContextPropertyID"><xsl:value-of select="$tns"></xsl:value-of><xsl:value-of select="$GPA_MD_P1"></xsl:value-of></xsl:attribute>
                        			</xsl:element>
                        		</xsl:when>
                        	</xsl:choose>
                        	<!-- cases end -->
                        </xsl:element>
                		<xsl:value-of select="$newline"></xsl:value-of>
                		<xsl:value-of select="$newline"></xsl:value-of>
                        
                        <xsl:comment>
                        	<xsl:text>Definition of When Constructs</xsl:text>
                        </xsl:comment>        
                		<xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:element name="masc-se:When">
                            <xsl:attribute name="MASCID">	<!-- MASCID="GPA_MD_WF-GPA_MD_WFMASCID" -->
                                <xsl:value-of select="$GPA_MD_WF"></xsl:value-of>
                                <xsl:text>-</xsl:text>
                                <xsl:value-of select="$GPA_MD_WFMASCID"></xsl:value-of>
                            </xsl:attribute>
                			<xsl:element name="masc-se:AllowedStates">
                			    <xsl:element name="masc-se:StateRef">
                			        <xsl:attribute name="To">	<!-- StateRef To="tns:GPA_MD_SD" -->
                			            <xsl:value-of select="$tns"></xsl:value-of>
                			            <xsl:value-of select="$GPA_MD_SD"></xsl:value-of>
                			        </xsl:attribute>
                			    </xsl:element>
                			</xsl:element>
                			<xsl:element name="masc-se:PossibleTriggerEvents">
                			    <xsl:element name="masc-se:EventRef">
                			        <xsl:attribute name="To">	<!-- EventRef To="tns:GPA_MD_ED" -->
                			            <xsl:value-of select="$tns"></xsl:value-of>
                			            <xsl:value-of select="$GPA_MD_ED"></xsl:value-of>
                			        </xsl:attribute>
                			    </xsl:element>
                			</xsl:element>
                        	<!-- TODO: ref to boolean expression -->
                        	<xsl:element name="masc-se:BooleanExpression">
                        		<xsl:element name="masc-se:BooleanExpressionRef">
                        			<!-- TODO: should work after tns done -->
                        			<xsl:attribute name="To"><xsl:value-of select="$tns"></xsl:value-of><xsl:value-of select="$xslt//masc-ex:BooleanExpression/@MASCID"></xsl:value-of></xsl:attribute>
                        		</xsl:element>
                        	</xsl:element>
                        </xsl:element>
                		<xsl:value-of select="$newline"></xsl:value-of>
                		<xsl:value-of select="$newline"></xsl:value-of>
                        
                        <!-- boolean expressions constructs -->
                		<xsl:comment> 
                			<xsl:text>Definition of optional BooleanExpressions for monitoring definition</xsl:text>
                		</xsl:comment>
                		<xsl:value-of select="$newline"></xsl:value-of>
                        <!-- TODO -->
                        <!-- could add a for loop for multiple When-FilteringCondition -->
                        <xsl:apply-templates select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$md-MASCID]/When-FilteringCondition"/>
                        
                        
                		<!-- GPA_MD_booleanExp cases start -->
                		
                		<!-- GPA_MD_booleanExp cases end -->
                		<xsl:value-of select="$newline"></xsl:value-of>
                		<xsl:value-of select="$newline"></xsl:value-of>
                		
                        <xsl:comment> 
                        	<xsl:text>Definition of Monitoring Data Collection</xsl:text>
                        </xsl:comment>
                		<xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:element name="masc-ap:MonitoredDataCollection">
                            <xsl:attribute name="MASCID">	<!-- MASCID="GPA_MD_MO-GPA_MD_MASCID" -->
                                <xsl:value-of select="$GPA_MD_MO"></xsl:value-of>
                                <xsl:text>-</xsl:text>
                                <xsl:value-of select="$GPA_MD_MASCID"></xsl:value-of>
                            </xsl:attribute>
                            <xsl:attribute name="PassedInSOAPHeaders">	<!-- GPA_MD_PassedSOAPHeaders="T/F" -->
                                <xsl:choose>
                                    <xsl:when test="contains(lower-case($GPA_MD_PassedSOAPHeaders),'true')">
                                        <xsl:text>true</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="contains(lower-case($GPA_MD_PassedSOAPHeaders),'false')">
                                        <xsl:text>false</xsl:text>
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:attribute>
                			<xsl:element name="masc-gp:MonitoredDataItemRef">
                			    <xsl:attribute name="To">	<!-- MonitoredDataItemRef To="tns:GPA_MD_MASCID" -->
                			        <xsl:value-of select="$tns"></xsl:value-of>
                			        <xsl:value-of select="$GPA_MD_MASCID"></xsl:value-of>
                			    </xsl:attribute>
                					<xsl:element name="masc-ap:ConfigurationData">    <!-- copy all context in configurationData -->
                					    <xsl:copy-of select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$md-MASCID]/ConfigurationData/*"></xsl:copy-of>
                					</xsl:element>
                			</xsl:element>
                        </xsl:element>
                		<xsl:value-of select="$newline"></xsl:value-of>
                		<xsl:value-of select="$newline"></xsl:value-of>
                        
                        <xsl:comment> 
                        	<xsl:text>Definition of Monitoring Action Groups</xsl:text>
                        </xsl:comment>
                		<xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:element name="masc-ap:ActionGroup">
                            <xsl:attribute name="MASCID">	<!-- MASCID="AG-GPA_MD_MO-GPA_MD_MASCID" -->
                                <xsl:text>AG-</xsl:text>
                                <xsl:value-of select="$GPA_MD_MO"></xsl:value-of>
                                <xsl:text>-</xsl:text>
                                <xsl:value-of select="$GPA_MD_MASCID"></xsl:value-of>
                            </xsl:attribute>
                            <xsl:element name="masc-gp:MonitoredDataCollectionRef">
                                <xsl:attribute name="To">	<!-- MonitoredDataCollectionRef To="tns:GPA_MD_MO-GPA_MD_MASCID" -->
                                    <xsl:value-of select="$tns"></xsl:value-of>
                                    <xsl:value-of select="$GPA_MD_MO"></xsl:value-of>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$GPA_MD_MASCID"></xsl:value-of>
                                </xsl:attribute>
                            </xsl:element>
                        </xsl:element>
                		<xsl:value-of select="$newline"></xsl:value-of>
                		<xsl:value-of select="$newline"></xsl:value-of>
                        
                        <xsl:comment> 
                        	<xsl:text>Definition of Action Policy Assertions configuring monitoring</xsl:text>
                        </xsl:comment>
                		<xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:element name="masc-ap:ActionPolicyAssertion">
                            <xsl:attribute name="MASCID">	<!-- MASCID="GPA_MD_APAM-GPA_MD_MASCID" -->
                                <xsl:value-of select="$GPA_MD_APAM"></xsl:value-of>
                                <xsl:text>-</xsl:text>
                                <xsl:value-of select="$GPA_MD_MASCID"></xsl:value-of>
                            </xsl:attribute>
                            <xsl:attribute name="ManagementParty">
                                <xsl:value-of select="$GPA_MD_MPTY"></xsl:value-of>
                            </xsl:attribute>
                            <xsl:element name="masc-se:WhenRef">
                                <xsl:attribute name="To">
                                    <xsl:value-of select="$tns"></xsl:value-of>
                                    <xsl:value-of select="$GPA_MD_WF"></xsl:value-of>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$GPA_MD_MASCID"></xsl:value-of>
                                </xsl:attribute>
                            </xsl:element>
                            <xsl:element name="masc-ap:ActionGroupRef">
                                <xsl:attribute name="To">	<!-- ActionGroupRef To="tns:AG-GPA_MD_MO-GPA_MD_MASCID" -->
                                    <xsl:value-of select="$tns"></xsl:value-of>
                                    <xsl:text>AG-</xsl:text>
                                    <xsl:value-of select="$GPA_MD_MO"></xsl:value-of>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$GPA_MD_MASCID"></xsl:value-of>
                                </xsl:attribute>
                            </xsl:element>
                        </xsl:element>
                		<xsl:value-of select="$newline"></xsl:value-of>    
                		<xsl:value-of select="$newline"></xsl:value-of>
                        <xsl:text disable-output-escaping="yes"><![CDATA[</wsp:Policy>]]></xsl:text>
                        <xsl:value-of select="$newline"></xsl:value-of>
 
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>      
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template match="masc-config:MonitoringDef-AdditionalInfo/When-FilteringCondition">
        <xsl:if test=".//Equal">
            <xsl:apply-templates select=".//Equal"/>
        </xsl:if>
        <xsl:if test=".//NotEqual">
            <xsl:apply-templates select=".//NotEqual"/>
        </xsl:if>
        <xsl:if test=".//Greater">
            <xsl:apply-templates select=".//Greater"/>
        </xsl:if>
        <xsl:if test=".//Less">
            <xsl:apply-templates select=".//Less"/>
        </xsl:if>
        <xsl:if test=".//GreaterOrEqual">
            <xsl:apply-templates select=".//GreaterOrEqual"/>
        </xsl:if>
        <xsl:if test=".//LessOrEqual">
            <xsl:apply-templates select=".//LessOrEqual"/>
        </xsl:if>
        
        <xsl:if test="./AND">
            <xsl:apply-templates select="./AND"/>
        </xsl:if>
        <xsl:if test="./OR">
            <xsl:apply-templates select="./OR"/>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="Equal">
        <xsl:text>I'm Equal</xsl:text>
        <xsl:value-of select="$newline"/>
    </xsl:template>
    
    <xsl:template match="NotEqual">
        <xsl:text>I'm NotEqual</xsl:text>
        <xsl:value-of select="$newline"/>
    </xsl:template>
    
    <xsl:template match="Greater">
        <xsl:text>I'm Greater</xsl:text>
        <xsl:value-of select="$newline"/>
    </xsl:template>
    
    <xsl:template match="Less">
        <xsl:text>I'm Less</xsl:text>
        <xsl:value-of select="$newline"/>
    </xsl:template>
    
    <xsl:template match="GreaterOrEqual">
        <xsl:text>I'm GreaterOrEqual</xsl:text>
        <xsl:value-of select="$newline"/>
    </xsl:template>
    
    <xsl:template match="LessOrEqual">
        <xsl:text>I'm LessOrEqual</xsl:text>
        <xsl:value-of select="$newline"/>
    </xsl:template>
    
    <xsl:template match="AND">
        <xsl:text>I'm AND</xsl:text>
        <xsl:value-of select="$newline"/>
    </xsl:template>
    
    <xsl:template match="OR">
        <xsl:text>I'm OR</xsl:text>
        <xsl:value-of select="$newline"/>
    </xsl:template>

</xsl:stylesheet>